package com.example.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lin.entity.pojo.Department;
import com.example.lin.entity.vo.DepartmentVo;
import com.example.lin.mapper.DepartmentMapper;
import com.example.lin.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 林志豪
 * @since 2024-02-09
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public Page<DepartmentVo> pageVo(Page page, DepartmentVo departmentVo) {
        Page page1 = this.page(page, new QueryWrapper<Department>().lambda().like(Department::getName, departmentVo.getName()));
        return null;
    }

    /**
     * 递归子部门
     * @param departments   部门信息
     * @param pid           父级部门ID
     * @return
     */
    @Override
    public List<Department> recursion(List<Department> departments, Integer pid) {
        List<Department> list = new ArrayList<>();
        List<Department> collect = Optional.ofNullable(departments).orElse(new ArrayList<Department>()).stream().filter(item -> !ObjectUtils.isEmpty(item) && item.getId().equals(pid)).collect(Collectors.toList());
        collect.forEach(item -> {
            Department department = new Department();
            BeanUtils.copyProperties(item, department);
            List<Department> children = recursion(departments, item.getId());
            item.setChildren(children);
            list.add(item);
        });
        return list;
    }
}
